#include <input_manager.h>
#include <tslib.h>
#include <stdio.h>

struct tsdev *ts;



static int TouchInin(void)
{
	ts = ts_setup(NULL, 0);
	if (!ts)
	{
		printf("ts_setup err\n");
		return -1;
	}
    return 0;
}

static int TouchExit(void)
{
    ts_close(ts);
    return 0;
}


static int GetTouchEvent(PInputEvent ptInputEvent)
{
	struct ts_sample samp;
	int ret;

	ret = ts_read(ts, &samp, 1);
    
	if (ret != 1)
		return -1;    

    ptInputEvent->iType   = INPUT_TYPE_TOUCH;
    ptInputEvent->ix     = samp.x;
    ptInputEvent->iy     = samp.y;
    ptInputEvent->iPress = samp.pressure;
    ptInputEvent->tTime  = samp.tv;

    return 0;
}



static InputDevice g_tTouchScreenDev = {
    .name = "touchscreen",
    .GetInputEvent = GetTouchEvent,
    .InputInin     = TouchInin,
    .InputExit     = TouchExit,  
};


void TouchInPutDevRegister(void)
{
    RegisterInPut(&g_tTouchScreenDev); 
}
#if 0

int main(int argc, char **argv)
{
    InputEvent event;
    int ret;
    g_tTouchScreenDev.InputInin();

    while(1)
    {
        g_tTouchScreenDev.GetInputEvent(&event);
        if(ret)
        {
            printf("Get Event Error\n");
            return -1;
        }
        else
        {
            printf("Type = %d\n",event.iType);
            printf("X = %d\n",event.ix);
            printf("Y = %d\n",event.iy);
            printf("Press = %d\n",event.iPress);

        }
    }
    return 0;
}
#endif
